package com.mew.slots.core.game.zzdzz.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.zzdzz.dto.ZzdzzInRoom;
import com.mew.slots.core.game.zzdzz.dto.ZzdzzInRoomList;
import com.mew.slots.core.game.zzdzz.dto.ZzdzzPlayMsgs;
import com.mew.slots.core.game.zzdzz.roller.Controlline;

import java.util.ArrayList;

public class ZzdzzRoom extends BaseRoom {

    public static int _SCATTER = 9;
    public static int _FREE = 8;
    public int progress_max = 100;
    public int progress_add = 1;

    private int[] courseTypes={1,1,2,0,1,1,2,0,1,1,1,2,0,1,1,1,1,2};//0 转盘（此次清除银行数据） 1 转盘 2 free

    private int[] jpPro={99874,10,5,3,2};
/*    private int[] jpPro={20,10,8,6,4};*/
    private int[] coinRates={10,20,30,40,50};

    private ZzdzzInRoomList zzdzzInRoomList;
    private ZzdzzInRoom zzdzzInRoom=null;
    private int freeType;
    private int freeNum;
    public ZzdzzRoom(ZzdzzInRoomList zzdzzInRoomList,int freeTyp,int freeNum){
        this.zzdzzInRoomList=zzdzzInRoomList;
        this.freeType=freeTyp;
        this.freeNum=freeNum;
        res = new ZzdzzPlayMsgs();
        row = 4;
        col = 6;
        _WILD = 7;
        spes = new ArrayList<Integer>(){{
            add(-1);
            add(8);
            add(9);
        }};

         rate = new int[][]{
                 {0,  0,  5,   15, 30,  80},//钞票                 0
                 {0,  0,  5,   15, 30,  80},//金砖                 1
                 {0,  0,  10,  25, 100, 120},//钥匙                 2
                 {0,  0,  10,  25, 100, 120},//保险箱               3
                 {0,  0,  10,  30, 120, 150},//狗                  4
                 {0,  0,  15,  45, 150, 200},//保安                 5
                 {0,  0,  20,  50, 200, 300},//银行                 6
                 {0,  0,  0,   0,   0,   0},//_WILD                7
                 {0,  0,  0,   0,   0,   0},//_FREE 金币            8
                 {0,  0,  0,   0,   0,   0} //_SCATTER 存钱罐        9
        };
    }
    /**
     * 取线
     */
    @Override
    public void getLinesXml(){
        for (ZzdzzInRoom vo : zzdzzInRoomList.getZzdzzInRoomList()) {
            if(vo.getBet()==getRes().getBet()){
                zzdzzInRoom=vo;
            }
        }
        if(zzdzzInRoom==null){
            zzdzzInRoom=new ZzdzzInRoom();
            zzdzzInRoom.setBet(getRes().getBet());
            zzdzzInRoomList.getZzdzzInRoomList().add(zzdzzInRoom);
        }

        if (controlType>0){
            endSlots = control();
        } else {
            int path=0;
            if(free){
                if(freeType==2){
                    path=2;
                }else{
                    path=1;
                }
            }
            endSlots = rollerXml.getLineInts(path,rollerID);
        }
        replaceRC();
        res.endSlots = endSlots;
    }
    private void replaceRC(){
        if(free&&freeType>0){
            if(!zzdzzInRoom.getWes().contains(2)){
                replaceC();
            }
            if(!zzdzzInRoom.getWes().contains(3)){
                replaceR();
            }
        }else{
            replaceR();
            replaceC();
        }
    }
    private void replaceC(){
        for (int r = 1; r <= row; r++) {
            endSlots[r*col-1]=-1;
        }
    }
    private void replaceR(){
        for (int c = 0; c < col; c++) {
            endSlots[c]=-1;
        }
    }

    @Override
    public void freeBouns() {
        if(!isFree()||(isFree()&&freeType==0)) {
            //奖池
            if (controlType == 4) {
                res.setJp(RandomUtil.randomInt(1, 5));
            } else {
                res.setJp(GameUtil.weights(jpPro));
            }
        }

        if(zzdzzInRoom.getProgressBar()==progress_max){
            zzdzzInRoom.setProgressBar(0);
        }
        ZzdzzPlayMsgs pmres=(ZzdzzPlayMsgs) res;
        if(free){
            computeScatterWin(pmres);
        }else {
            int number = findFree(pmres);
            if (number > 0) {
                if (number < 5) {
                    hasBonus(pmres,number);
                } else {
                    computeFree(pmres);
                }
            }
        }
        pmres.setProgressBar(zzdzzInRoom.getProgressBar());
        pmres.setCourse(zzdzzInRoom.getCourse());
        zzdzzInRoomList.setFreeCoins(pmres.getFreeCoins());
        if(zzdzzInRoom.getCourse()==courseTypes.length&&freeNum==1){
            zzdzzInRoom.setBankIndex(0);
            zzdzzInRoom.setCourse(0);
            zzdzzInRoom.getWes().clear();
        }
    }
    private int findFree(ZzdzzPlayMsgs pmres){
        int num=0;
        for (int i = 0; i < endSlots.length; i++) {
            if(endSlots[i]==_FREE){
                num++;
                pmres.getFreeCoins()[i]=computeFreeCoin();
            }
        }
        return num;
    }
    private long computeFreeCoin(){
        return getRes().getBet()*getRes().getLine()*coinRates[RandomUtil.randomInt(coinRates.length)]/10;
    }
    private void hasBonus(ZzdzzPlayMsgs pmres, int number){
        int pb=zzdzzInRoom.getProgressBar()+number*progress_add;
        if(pb>=progress_max){
            computeBonus(pmres);
            pb=progress_max;
        }
        zzdzzInRoom.setProgressBar(pb);
    }
    private void computeBonus(ZzdzzPlayMsgs pmres){
        int ct=courseTypes[zzdzzInRoom.getCourse()];
        if(ct==2){//特殊免费
            pmres.setFree((byte)1);
            zzdzzInRoom.setProgressBar(0);
            if(zzdzzInRoom.getWes().contains(0)){
                pmres.setFreeNumber(10);
            }else{
                pmres.setFreeNumber(5);
            }
            if(zzdzzInRoom.getWes().contains(2)){
                pmres.setCol(1);
            }
            if(zzdzzInRoom.getWes().contains(3)){
                pmres.setRow(1);
            }
            if(zzdzzInRoom.getWes().contains(4)){
                pmres.setFreeType(2);
            }else{
                pmres.setFreeType(1);
            }
            zzdzzInRoomList.setScatterCoin(zzdzzInRoom.getWeAddCoin()+getRes().getBet()*getRes().getLine());
            pmres.setScatterCoin(zzdzzInRoomList.getScatterCoin());
        }else{
            pmres.setBouns(1);
            long[] roulettes=computeRoulettes();
            pmres.setRoulettes(roulettes);
            zzdzzInRoom.setRoulettes(roulettes);
            if(ct==0){
                zzdzzInRoom.setBankIndex(zzdzzInRoom.getBankIndex()+1);
                zzdzzInRoom.getWes().clear();
                zzdzzInRoom.setWeAddCoin(0);
            }
        }
        zzdzzInRoom.setCourse(zzdzzInRoom.getCourse()+1);
    }



    private long[] computeRoulettes(){
        return new long[]{
                computeRoulette(),computeRoulette(),computeRoulette(),computeRoulette()
        };
    }
    private long computeRoulette(){
        return getRes().getBet()*getRes().getLine()* RandomUtil.randomInt(5,9);
    }
    private void computeFree(ZzdzzPlayMsgs pmres){
        pmres.setFree((byte)1);

        //计算猪猪金币
        long scatterCoin=0;
        for(long fc:pmres.getFreeCoins()){
            scatterCoin+=fc;
        }

        pmres.setFreeNumber(5);
        zzdzzInRoomList.setScatterCoin(scatterCoin);
        pmres.setScatterCoin(zzdzzInRoomList.getScatterCoin());
    }
    private void computeScatterWin(ZzdzzPlayMsgs pmres){
        int snum=findScatter(pmres);
        res.setBounswin(snum*zzdzzInRoomList.getScatterCoin());
    }
    private int findScatter(ZzdzzPlayMsgs pmres){
        int num=0;
        int freeNum=0;
        for (int i = 0; i < endSlots.length; i++) {
            if(endSlots[i]==_SCATTER){
                num++;
                //if(RandomUtil.randomInt(3)==0) {
                if(false){
                    int rfn = RandomUtil.randomInt(1, 4);
                    freeNum += rfn;
                    pmres.getFreeCoins()[i] = rfn;
                }
            }
        }
        if(freeNum>0){
            pmres.setFree((byte)1);
            pmres.setFreeNumber(freeNum);
        }
        return num;
    }

    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return controlline.big();
            case 2:
                return controlline.free();
            case 3:
                return controlline.bonuds();
            default:
                return controlline.empty();
        }
    }
}
